#!/bin/bash

set -x

# default to update source then build

TOP_DIR=$(pwd)
SRC_DIR=$TOP_DIR/lib_source
BD_DIR=$TOP_DIR/lib_build
BIN_DIR=$TOP_DIR/lib_bin

export PATH=$TOP_DIR/cross/bin:$TOP_DIR/lib_build:$PATH

cmd=all64
if [ -n "$1" ] ; then
	cmd=$1
fi

cat > $TOP_DIR/lib_build/uname << EOF
#!/bin/bash

echo "MINGW32_NT-5.1"

EOF
chmod +x $TOP_DIR/lib_build/uname

if [ "$cmd" == "all64" ] || [ "$cmd" == "all32" ] || [ "$cmd" == "update" ] ; then
	mkdir -p $SRC_DIR || exit 0
	cd $SRC_DIR
	if [ ! -f fltk-1.1.10-source.tar.bz2 ] ; then
		wget http://ftp.funet.fi/pub/mirrors/ftp.easysw.com/pub/fltk/1.1.10/fltk-1.1.10-source.tar.bz2
	fi
	cd ..
fi

if [ "$cmd" == "all64" ] || [ "$cmd" == "b64" ] || [ "$cmd" == "all32" ] || [ "$cmd" == "b32" ] ; then
	HOST_FLAG="--host=x86_64-w64-mingw32"
	ENV_FLAG="ac_cv_path_AR=x86_64-w64-mingw32-ar"
	if [ "$cmd" == "all32" ] || [ "$cmd" == "b32" ] ; then
		HOST_FLAG="--host=i686-w64-mingw32"
		ENV_FLAG="ac_cv_path_AR=i686-w64-mingw32-ar"
	fi
	rm -rf $BIN_DIR/fltk
	mkdir -p $BD_DIR
	cd $BD_DIR
	rm -rf bd_fltk
	tar xjf $SRC_DIR/fltk-1.1.10-source.tar.bz2
	mv fltk-1.1.10 bd_fltk
	cd bd_fltk
	patch -p1 < $TOP_DIR/patch/fltk-w64.patch

	# actually we don't enable cygwin, but only disable add '-mno-cygwin' gcc option
	./configure $HOST_FLAG --prefix=$BIN_DIR/fltk \
		--enable-cygwin \
		--enable-threads --enable-largefile $ENV_FLAG
		

#	--enable-static --disable-shared
#	--enable-shared --disable-static

	make $MAKE_OPT 
	make install
	cd ..
	cd ..
fi

rm $TOP_DIR/lib_build/uname

